// 测试数据
const testData = {
    courses: [
        {
            id: 1,
            title: 'Fundamentals of Python Programming',
            description: 'Beginner level Python programming with zero foundation, mastering basic programming knowledge',
            coverImage: 'images/courses/python.jpg',
            teacher: 'Teacher Zhang',
            enrollCount: 1200,
            price: 0,
            recommended: true,
            createDate: '2025-01-01'
        },
        {
            id: 2,
            title: 'Introduction to Web Development',
            description: 'Learn the basics of HTML, CSS, and JavaScript',
            coverImage: 'images/courses/web.jpg',
            teacher: 'Miss Li',
            enrollCount: 980,
            price: 0,
            recommended: true,
            createDate: '2025-01-02'
        },
        {
            id: 3,
            title: 'higher mathematics',
            description: 'College Mathematics Fundamentals Course, Calculus and Linear Algebra',
            coverImage: 'images/courses/math.jpg',
            teacher: 'Teacher Wang',
            enrollCount: 750,
            price: 0,
            recommended: false,
            createDate: '2025-01-03'
        }
    ],
    courseContents: [
        {
            id: 1,
            courseId: 1,
            title: 'Python environment setup',
            type: 'video',
            url: 'videos/python/setup.mp4'
        },
        {
            id: 2,
            courseId: 1,
            title: 'Basic syntax of Python',
            type: 'document',
            url: 'docs/python/basics.pdf'
        }
    ],
    comments: [
        {
            id: 1,
            courseId: 1,
            userId: 1,
            username: 'Xiao Ming',
            content: 'The course explanation is very clear, very suitable for beginners!',
            createTime: '2025-01-05 10:30:00'
        }
    ]
};

// 测试数据生成器
class TestDataGenerator {
    constructor() {
        this.init();
    }

    async init() {
        await this.generateUsers();
        await this.generateCourses();
        await this.generateCourseContents();
        await this.generateComments();
        await initTestData();
    }

    // 生成用户数据
    async generateUsers() {
        const users = [
            {
                username: 'admin',
                password: 'admin123',
                email: 'admin@example.com',
                role: 'admin',
                createDate: '2025-01-01T00:00:00.000Z'
            },
            {
                username: 'teacher1',
                password: 'teacher123',
                email: 'teacher1@example.com',
                role: 'teacher',
                createDate: '2025-01-02T00:00:00.000Z'
            },
            {
                username: 'teacher2',
                password: 'teacher123',
                email: 'teacher2@example.com',
                role: 'teacher',
                createDate: '2025-01-03T00:00:00.000Z'
            },
            {
                username: 'student1',
                password: 'student123',
                email: 'student1@example.com',
                role: 'student',
                createDate: '2025-01-04T00:00:00.000Z'
            },
            {
                username: 'student2',
                password: 'student123',
                email: 'student2@example.com',
                role: 'student',
                createDate: '2025-01-05T00:00:00.000Z'
            }
        ];

        for (const user of users) {
            try {
                await db.add('users', user);
            } catch (error) {
                console.log('The user already exists:', user.username);
            }
        }
    }

    // 生成课程数据
    async generateCourses() {
        const courses = [
            {
                id: 1,
                title: 'Fundamentals of Python Programming',
                description: 'Beginner level Python programming with zero foundation, mastering basic programming knowledge',
                coverImage: 'images/courses/cs-1.jpg',
                teacher: 'Teacher Zhang',
                enrollCount: 1200,
                price: 0,
                recommended: true,
                createDate: '2025-01-01'
            },
            {
                id: 2,
                title: 'Introduction to Web Development',
                description: 'Learn the basics of HTML, CSS, and JavaScript',
                coverImage: 'images/courses/cs-2.jpg',
                teacher: 'Miss Li',
                enrollCount: 980,
                price: 0,
                recommended: true,
                createDate: '2025-01-02'
            },
            {
                id: 3,
                title: 'higher mathematics',
                description: 'College Mathematics Fundamentals Course, Calculus and Linear Algebra',
                coverImage: 'images/courses/math-1.jpg',
                teacher: 'Teacher Wang',
                enrollCount: 750,
                price: 0,
                recommended: false,
                createDate: '2025-01-03'
            }
        ];

        for (const course of courses) {
            try {
                const existingCourse = await db.get('courses', course.id);
                if (!existingCourse) {
                    await db.add('courses', course);
                }
            } catch (error) {
                console.error('Failed to add course:', error);
            }
        }
    }

    // 生成课程内容数据
    async generateCourseContents() {
        const courseContents = [
            {
                id: 1,
                courseId: 1,
                title: 'Python environment setup',
                type: 'video',
                url: 'videos/python/setup.mp4'
            },
            {
                id: 2,
                courseId: 1,
                title: 'Basic syntax of Python',
                type: 'document',
                url: 'docs/python/basics.pdf'
            },
            {
                id: 3,
                courseId: 2,
                title: 'HTML Fundamentals',
                type: 'video',
                url: 'videos/web/html.mp4'
            },
            {
                id: 4,
                courseId: 2,
                title: 'CSS Styles',
                type: 'document',
                url: 'docs/web/css.pdf'
            },
            {
                id: 5,
                courseId: 3,
                title: 'Limit and Continuity',
                type: 'video',
                url: 'videos/math/limit.mp4'
            },
            {
                id: 6,
                courseId: 3,
                title: 'derivative and differentiate',
                type: 'document',
                url: 'docs/math/derivative.pdf'
            }
        ];

        for (const content of courseContents) {
            try {
                const existingContent = await db.get('courseContents', content.id);
                if (!existingContent) {
                    await db.add('courseContents', content);
                }
            } catch (error) {
                console.error('Failed to add course content:', error);
            }
        }
    }

    // 生成评论数据
    async generateComments() {
        const comments = [
            {
                id: 1,
                courseId: 1,
                userId: 1,
                username: 'student1',
                content: 'The Python course explanation is very clear and very suitable for beginners!',
                createTime: '2025-01-05 10:30:00'
            },
            {
                id: 2,
                courseId: 2,
                userId: 2,
                username: 'student2',
                content: 'The content of the web development course is very practical and I have learned a lot.',
                createTime: '2025-01-06 15:20:00'
            },
            {
                id: 3,
                courseId: 3,
                userId: 1,
                username: 'student1',
                content: 'The math explanation is easy to understand, and the accompanying exercises are very helpful.',
                createTime: '2025-01-07 09:45:00'
            }
        ];

        for (const comment of comments) {
            try {
                const existingComment = await db.get('comments', comment.id);
                if (!existingComment) {
                    await db.add('comments', comment);
                }
            } catch (error) {
                console.error('Adding comment failed:', error);
            }
        }
    }
}

// 初始化测试数据
async function initTestData() {
    try {
        // 等待数据库初始化
        while (!db.db) {
            await new Promise(resolve => setTimeout(resolve, 100));
        }

        // 检查并添加课程数据
        for (const course of testData.courses) {
            const existingCourse = await db.get('courses', course.id);
            if (!existingCourse) {
                await db.add('courses', course);
            }
        }
        
        // 检查并添加课程内容
        for (const content of testData.courseContents) {
            const existingContent = await db.get('courseContents', content.id);
            if (!existingContent) {
                await db.add('courseContents', content);
            }
        }
        
        // 检查并添加评论
        for (const comment of testData.comments) {
            const existingComment = await db.get('comments', comment.id);
            if (!existingComment) {
                await db.add('comments', comment);
            }
        }

        console.log('Test data initialization completed');
    } catch (error) {
        console.error('Failed to initialize test data:', error);
    }
}

// 初始化测试数据
window.addEventListener('load', () => {
    setTimeout(() => {
        new TestDataGenerator();
    }, 1000); // 等待数据库初始化完成
});
